// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.database.figures;

import org.dengues.commons.IDenguesSharedImage;
import org.dengues.commons.utils.ImageUtil;
import org.dengues.designer.ui.database.commands.DBGefChangeSelectedCommand;
import org.dengues.model.database.DBColumn;
import org.dengues.model.database.EMFDataType;
import org.eclipse.draw2d.AbstractBorder;
import org.eclipse.draw2d.ButtonModel;
import org.eclipse.draw2d.ChangeEvent;
import org.eclipse.draw2d.ChangeListener;
import org.eclipse.draw2d.CheckBox;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.ImageFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.Shape;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class DBColumnCustomFigure extends Shape {

    private final DBColumn dbColumn;

    private CheckBox isSelected;

    private Label columnName;

    private ImageFigure fig;

    private CommandStack commandStack;

    /**
     * Qiang.Zhang.Adolf@gmail.com DBColumnCustomFigure constructor comment.
     * 
     * @param column
     */
    public DBColumnCustomFigure(DBColumn column) {
        this.dbColumn = column;
        org.eclipse.draw2d.FlowLayout myGenLayoutManager = new org.eclipse.draw2d.FlowLayout();
        myGenLayoutManager.setStretchMinorAxis(false);
        myGenLayoutManager.setMinorAlignment(org.eclipse.draw2d.FlowLayout.ALIGN_CENTER);
        myGenLayoutManager.setMajorAlignment(org.eclipse.draw2d.FlowLayout.ALIGN_LEFTTOP);
        myGenLayoutManager.setMajorSpacing(5);
        myGenLayoutManager.setMinorSpacing(5);
        myGenLayoutManager.setHorizontal(true);
        setLayoutManager(myGenLayoutManager);
        setBorder(new ColumnFigureBorder());
        createContents();
    }

    public void createContents() {
        removeAll();
        if (!dbColumn.getName().equals("*")) { //$NON-NLS-1$
            isSelected = new CheckBox();
            final int emfType = dbColumn.getTable().getDbDiagram().getEmfType().getValue();
            switch (emfType) {
            case EMFDataType.DATABASE:
                isSelected.setSelected(dbColumn.isKey());
                break;
            case EMFDataType.QUERY:
                isSelected.setSelected(dbColumn.isSelected());
                break;
            }
            isSelected.addChangeListener(new ChangeListener() {

                public void handleStateChanged(ChangeEvent event) {
                    if (event.getPropertyName().equals(ButtonModel.SELECTED_PROPERTY)) {
                        getCommandStack().execute(new DBGefChangeSelectedCommand(dbColumn, isSelected.isSelected(), emfType));
                    }
                }

            });
            add(isSelected);
        }
        if (dbColumn.isKey()) {
            fig = new ImageFigure(ImageUtil.getImage(IDenguesSharedImage.TABLEVIEW_KEY));
        } else if (dbColumn.isForeignKey()) {
            fig = new ImageFigure(ImageUtil.getImage(IDenguesSharedImage.VIEW_PK));
        } else {
            fig = new ImageFigure(ImageUtil.getImage(IDenguesSharedImage.VIEW_COLUMN));
        }
        add(fig);
        columnName = new Label();

        columnName.setText(dbColumn.getName());
        Font erFont = new Font(null, "Arial", 8, SWT.BOLD); //$NON-NLS-1$
        columnName.setFont(erFont);
        columnName.setForegroundColor(ColorConstants.blue);
        add(columnName);
    }

    @Override
    protected void fillShape(Graphics graphics) {
        Rectangle r = getBounds();

        Rectangle.SINGLETON.setBounds(r);
        Rectangle.SINGLETON.y += 1;
        Rectangle.SINGLETON.height -= 1;

        int shadowDepth = 0;
        int rectangleWidth = r.width - shadowDepth;
        int rectangleHeight = r.height - shadowDepth;

        Color foreground = graphics.getForegroundColor();
        Color background = graphics.getBackgroundColor(), shadow = ColorUtilities.darker(foreground);

        int x = r.x + shadowDepth;
        int y = r.y + shadowDepth;

        /**
         * trace shadow
         */
        for (int i = shadowDepth - 1; i >= 0; i--) {
            // shadow = lighter(background, i + 1, shadowDepth + 1);
            graphics.setBackgroundColor(shadow);
            graphics.fillRectangle(x, y, rectangleWidth, rectangleHeight);
            if (i > 0) {
                x--;
                y--;
            }
        }

        graphics.setBackgroundColor(background);
        graphics.setForegroundColor(foreground);

        graphics.fillRectangle(x, y, rectangleWidth, rectangleHeight);
        // Gradient
        graphics.setForegroundColor(foreground);
        graphics.fillGradient(x, y - 20, rectangleWidth, columnName.getBounds().height + 25, true);
        graphics.setForegroundColor(foreground);

        graphics.drawRectangle(x, y - 1, rectangleWidth - 1, rectangleHeight);

    }

    @Override
    protected void outlineShape(Graphics graphics) {

    }

    class ColumnFigureBorder extends AbstractBorder {

        public Insets getInsets(IFigure figure) {
            return new Insets(5, 3, 5, 3);
        }

        public void paint(IFigure figure, Graphics graphics, Insets insets) {
            graphics.setForegroundColor(ColorConstants.blue);
            graphics.drawLine(getPaintRectangle(figure, insets).getTopLeft(), tempRect.getTopRight());
        }
    }

    /**
     * Getter for columnName.
     * 
     * @return the columnName
     */
    public Label getColumnName() {
        return this.columnName;
    }

    /**
     * Sets the isSelected.
     * 
     * @param isSelected the isSelected to set
     */
    public void setIsSelected(boolean isSelected) {
        this.isSelected.setSelected(isSelected);
    }

    /**
     * Getter for commandStack.
     * 
     * @return the commandStack
     */
    public CommandStack getCommandStack() {
        return this.commandStack;
    }

    /**
     * Sets the commandStack.
     * 
     * @param commandStack the commandStack to set
     */
    public void setCommandStack(CommandStack commandStack) {
        this.commandStack = commandStack;
    }

}
